/**
 * 地址管理API工具函数
 */

// 基础URL
const BASE_URL = 'http://localhost:8080/zmhshop';

/**
 * 获取地址列表
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getAddressList(params = {}) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/address/list`,
      method: 'GET',
      data: params,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`HTTP错误: ${res.statusCode}`));
        }
      },
      fail: (err) => {
        reject(new Error('网络请求失败'));
      }
    });
  });
}

/**
 * 根据ID获取地址详情
 * @param {Number} id 地址ID
 * @returns {Promise}
 */
export function getAddressById(id) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/address/${id}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`HTTP错误: ${res.statusCode}`));
        }
      },
      fail: (err) => {
        reject(new Error('网络请求失败'));
      }
    });
  });
}

/**
 * 新增地址
 * @param {Object} addressData 地址数据
 * @returns {Promise}
 */
export function addAddress(addressData) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/address`,
      method: 'POST',
      data: addressData,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`HTTP错误: ${res.statusCode}`));
        }
      },
      fail: (err) => {
        reject(new Error('网络请求失败'));
      }
    });
  });
}

/**
 * 更新地址
 * @param {Object} addressData 地址数据
 * @returns {Promise}
 */
export function updateAddress(addressData) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/address`,
      method: 'PUT',
      data: addressData,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`HTTP错误: ${res.statusCode}`));
        }
      },
      fail: (err) => {
        reject(new Error('网络请求失败'));
      }
    });
  });
}

/**
 * 删除地址
 * @param {Array} ids 地址ID数组
 * @returns {Promise}
 */
export function deleteAddress(ids) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/address/${ids.join(',')}`,
      method: 'DELETE',
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`HTTP错误: ${res.statusCode}`));
        }
      },
      fail: (err) => {
        reject(new Error('网络请求失败'));
      }
    });
  });
}

/**
 * 格式化地址显示
 * @param {Object} address 地址对象
 * @returns {String} 格式化后的地址字符串
 */
export function formatAddress(address) {
  if (!address) return '';
  
  const { province, city, county, content } = address;
  return `${province || ''}${city || ''}${county || ''}${content || ''}`;
}

/**
 * 获取默认地址
 * @returns {Object} 默认地址对象
 */
export function getDefaultAddress() {
  return {
    id: 1,
    province: '北京市',
    city: '北京市',
    county: '东城区',
    content: '默认地址',
    userName: '用户',
    phone: '17550066'
  };
}

